unit events;

interface
uses CoreI, sdl;
Procedure Core_RegisterEventHandler(const EventType:byte; EventHandler:TCore_EventHandler); cdecl;
procedure Core_UnregisterEventHandler(const handler:TCore_EventHandler); cdecl;
Procedure ProcessEvents;
Procedure Events_Free;
implementation
uses lists, common;
var eventHandlers:array[0..SDL_NUMEVENTS] of PList;
Procedure Core_RegisterEventHandler(const EventType:byte; EventHandler:TCore_EventHandler); cdecl;
begin
if eventtype >32 then exit;
list_add(EventHandlers[eventtype], @EventHandler);
end;
procedure Core_UnregisterEventHandler(const handler:TCore_EventHandler); cdecl;
var PWalk:PList;
i:integer;
begin
for i:= 0 to sdl_numevents do begin
PWalk:=EventHandlers[i];
while PWalk<>nil do begin
if PWalk^.Data = @handler then begin
list_removenode(EventHandlers[i], PWalk);
exit;
end else
PWalk:=PWalk^.Next;
end;
end;
end;
Procedure ProcessEvents;
Procedure ProcessEvent(event:TSDL_Event);
var PWalk:PList;
begin
            PWalk:=EventHandlers[event.type_];
            while PWalk<>nil do begin
if not TCore_EventHandler(PWalk^.Data)(event) then break;
PWalk:=PWalk^.Next;
                               end;
end;
var event:TSDL_Event;
begin
while ( SDL_PollEvent( @event ) > 0 ) do begin
case event.type_ of
sdl_quitev:
core_SetState(Core_Terminating);
sdl_activeevent:
                if event.active.state and SDL_APPINPUTFOCUS  = SDL_APPINPUTFOCUS then begin
if event.active.gain =0 then
core_setstate(core_suspended)
else
core_setstate(core_laststate);
end;
end;
ProcessEvent(event);
end;
end;
Procedure Events_Free;
var i:integer;
begin
for i:=0 to sdl_numevents do
List_Clear(EventHandlers[i]);
end;
                 end.
